package io.github.uicdb.gravity_generator;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.EnergyStorage;

import java.util.List;
import java.util.Set;

public class GGUtils {
    public static final String MODID="gravity_generator";
    public static final ResourceLocation EMPTY=new ResourceLocation("empty");
    public static void transferEnergy(EnergyStorage energyStorageHave, Set<Direction> directionList, Level level, BlockPos fromPos){
        if(level instanceof ServerLevel serverLevel) {
            directionList.forEach(direction -> {
                BlockPos otherPos = fromPos.relative(direction);
                BlockEntity beOther = serverLevel.getBlockEntity(otherPos);
                if (beOther != null) {
                    beOther.getCapability(CapabilityEnergy.ENERGY,direction.getOpposite()).ifPresent(ie->{
                        if (ie.canReceive()&&energyStorageHave.getEnergyStored()>0){
                            int extract= ie.receiveEnergy(energyStorageHave.getEnergyStored(),true);
                            ie.receiveEnergy(energyStorageHave.extractEnergy(extract,false),false );
                        }
                    });
                }
            });
        }
    }
}
